from django.db import models

from django.db import models

# # 自定义用户类
# class User(models.Model):
#     # 用户名字段，最大长度为100，必须唯一
#     username = models.CharField(max_length=100, unique=True)
#     # 密码字段，最大长度为32
#     password = models.CharField(max_length=128)
#     # 邮箱字段，标准的邮箱类型
#     email = models.EmailField(unique=False)
#     # 角色字段，用于区分管理员、种植员、检测员
#     role = models.CharField(
#         max_length=10,
#         choices=[('admin', 'Admin'), ('planter', 'Planter'), ('inspector', 'Inspector')],
#         default='planter'
#     )

#     last_login = models.DateTimeField(auto_now=True) 

#     # 手动添加 REQUIRED_FIELDS
#     REQUIRED_FIELDS = ['email', 'role']  # 这里的字段是创建用户时需要填写的字段

#     class Meta:
#         db_table = "user"

#     def __str__(self):
#         return self.username

from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    # 添加自定义的字段
    role = models.CharField(
        max_length=10,
        choices=[('admin', 'Admin'), ('planter', 'Planter'), ('inspector', 'Inspector')],
        default='planter'
    )

    # 不需要定义 REQUIRED_FIELDS，因为 AbstractUser 已经定义了它
    class Meta:
        db_table = "user"

    def __str__(self):
        return self.username


# 种植员模型
class Planter(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='planter_profile')
    contact_info = models.CharField(max_length=100, null=True, blank=True)
    
    def __str__(self):
        return f"Planter: {self.user.username}"


# 检测员模型
class Inspector(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='inspector_profile')
    contact_info = models.CharField(max_length=100, null=True, blank=True)
    
    def __str__(self):
        return f"Inspector: {self.user.username}"


# 作物模型
class Crop(models.Model):
    name = models.CharField(max_length=100)
    crop_type = models.CharField(max_length=50)
    planting_date = models.DateField()
    location = models.CharField(max_length=100)
    planter = models.ForeignKey(Planter, on_delete=models.CASCADE, related_name='crops')

    def __str__(self):
        return self.name


# 日志模型，记录作物的生长信息
class Log(models.Model):
    planting_date = models.DateField()
    quantity = models.IntegerField()
    location = models.CharField(max_length=100)
    crop = models.ForeignKey(Crop, on_delete=models.CASCADE, related_name='logs')

    def __str__(self):
        return f"Log for {self.crop.name}"


# 害虫模型
class Pest(models.Model):
    pest_type = models.CharField(max_length=100)
    cause = models.TextField()
    solution = models.TextField()

    def __str__(self):
        return self.pest_type


# 作物与害虫关联模型
class CropPest(models.Model):
    crop = models.ForeignKey(Crop, on_delete=models.CASCADE)
    pest = models.ForeignKey(Pest, on_delete=models.CASCADE)

    def __str__(self):
        return f"{self.crop.name} - {self.pest.pest_type}"


# 检测记录模型
class InspectionRecord(models.Model):
    inspection_date = models.DateField()
    inspection_type = models.CharField(max_length=100)
    result = models.TextField()
    crop = models.ForeignKey(Crop, on_delete=models.CASCADE)
    inspector = models.ForeignKey(Inspector, on_delete=models.CASCADE)

    def __str__(self):
        return f"Inspection for {self.crop.name} by {self.inspector.user.username}"
